home *** CD-ROM | disk | FTP | other *** search
/ Aminet 39 / Aminet 39 (2000)(Schatztruhe)[!][Oct 2000].iso / Aminet / game / shoot / Orbit_src.lha / Orbit / source / boom.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-07-04  |  5.1 KB  |  249 lines

  1. /*
  2.     Amiga port by Oliver Gantert
  3.  
  4.     27.04.2000 - fixed some compiler warnings
  5. */
  6. /*
  7.  
  8. ORBIT, a freeware space combat simulator
  9. Copyright (C) 1999  Steve Belczyk <steve1@genesis.nred.ma.us>
  10.  
  11. This program is free software; you can redistribute it and/or
  12. modify it under the terms of the GNU General Public License
  13. as published by the Free Software Foundation; either version 2
  14. of the License, or (at your option) any later version.
  15.  
  16. This program is distributed in the hope that it will be useful,
  17. but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19. GNU General Public License for more details.
  20.  
  21. You should have received a copy of the GNU General Public License
  22. along with this program; if not, write to the Free Software
  23. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  24.  
  25. */
  26.  
  27. #include "orbit.h"
  28.  
  29. void InitBooms()
  30. /*
  31.  *  Initiliaze explosions
  32.  */
  33. {
  34.   int i;
  35.   double v[3];
  36.  
  37.   /* Mark all booms unused */
  38.   for (i=0; i<NBOOMS; i++)
  39.   {
  40.     boom[i].age = 0.0;
  41.     boom[i].angle = 0.0;
  42.   }
  43.  
  44.   /* Tweak explosion data */
  45.   for (i=0; i<12; i++)
  46.   {
  47.     /* Tweak explosion shape */
  48.     Vmul (v, icos_data[i], 0.9+rnd (0.2));
  49.     Vmul (v, v, 0.2);
  50.     Vset (boom_data[i], v);
  51.  
  52.     /* Tweak explosion color */
  53.     boom_color[i][0] = 0.9+rnd (0.1);
  54.     boom_color[i][1] = rnd(1.5);
  55.   }
  56. }
  57.  
  58. int FindBoom()
  59. /*
  60.  *  Find unused or oldest boom
  61.  */
  62. {
  63.   int i, oldest;
  64.   double maxage;
  65.  
  66.   for (i=0; i<NBOOMS; i++)
  67.   {
  68.     if (boom[i].age == 0.0) return (i);
  69.  
  70.     if (i == 0)
  71.     {
  72.       maxage = boom[i].age;
  73.       oldest = i;
  74.     }
  75.     else
  76.     {
  77.       if (boom[i].age > maxage)
  78.       {
  79.         maxage = boom[i].age;
  80.         oldest = i;
  81.       }
  82.     }
  83.   }
  84.  
  85.   /* No unused booms, return oldest */
  86.   return (oldest);
  87. }
  88.  
  89. void DestroyBoom (int b)
  90. /*
  91.  *  Mark boom as unused
  92.  */
  93. {
  94.   boom[b].age = 0.0;
  95.  
  96.   /* Destroy corresponding light */
  97.   DestroyLight (boom[b].light);
  98. }
  99.  
  100. void Boom (double *v, double size)
  101. /*
  102.  *  There was an explosion at coordinates v[]
  103.  */
  104. {
  105.   int b, l;
  106.  
  107.   /* Find an unused boom */
  108.   b = FindBoom();
  109.  
  110.   /* Set palette_flash, so next screen clear flashes the screen */
  111.   palette_flash = 1;
  112.  
  113.   /* Set it up */
  114.   boom[b].age = deltaT;
  115.   Vset (boom[b].pos, v);
  116.   boom[b].size = size;
  117.  
  118.   l = FindLight();
  119.   boom[b].light = l;
  120.   light[l].pos[0] = (float) boom[b].pos[0];
  121.   light[l].pos[1] = (float) boom[b].pos[1];
  122.   light[l].pos[2] = (float) boom[b].pos[2];
  123.   light[l].age = deltaT;
  124.   light[l].color[0] = light[l].color[1] = light[l].color[2] = 1.0;
  125.  
  126.   boom[b].angle = rnd (360.0);
  127.  
  128.   /* Play sound */
  129.   if (sound) PlayAudio (SOUND_BOOM);
  130. }
  131.  
  132. void DrawBooms()
  133. /*
  134.  *  Draw the explosions
  135.  */
  136. {
  137.   int b;
  138.  
  139.   glPointSize (1.0);
  140.   for (b=0; b<NBOOMS; b++)
  141.   {
  142.     if (boom[b].age > 0.0) DrawBoom (b);
  143.   }
  144. }
  145.  
  146. void DrawBoom (int b)
  147. /*
  148.  *  Draw this explosion
  149.  */
  150. {
  151.   int i;
  152.   double v1[3], v2[3], v3[3], v[3];
  153.   double t, u;
  154.  
  155.   /* Bump boom time */
  156.   boom[b].age += deltaT;
  157.  
  158.   /* See if boom has expired */
  159.   if (boom[b].age > BOOM_TIME)
  160.   {
  161.     DestroyBoom (b);
  162.     return;
  163.   }
  164.  
  165.   /* Convert age to [0,1] */
  166.   t = boom[b].age / BOOM_TIME;
  167.  
  168.   /* And on [1,0] */
  169.   u = 1.0 - t;
  170.  
  171.   /* Set the intensity of this boom's light source */
  172.   i = boom[b].light;
  173.   light[i].color[0] = light[i].color[1] = light[i].color[2] = u;
  174.  
  175.   /* Draw the boom */
  176.   glPushMatrix();
  177.   glDisable (GL_LIGHTING);
  178.   glDepthMask (GL_FALSE);
  179.  
  180.   /* Translate to where the boom is */
  181.   Vsub (v, boom[b].pos, player.pos);
  182.   glTranslated (v[0], v[1], v[2]);
  183.  
  184.   /* Apply rotation so they all don't look the same */
  185.   glRotated (boom[b].angle, 1.0, 0.0, 1.0);
  186.  
  187.   /* Scale according to size of boom */
  188.   glScaled (boom[b].size, boom[b].size, boom[b].size);
  189.  
  190.   /* Draw a smaller yellow boom first */
  191.   glColor3f ((float)u, (float)u, 0.0);
  192.   if (t < 0.5)
  193.   /* Growing sphere */
  194.   glutSolidSphere (t/8.0, 5, 5);
  195.   else
  196.   /* Shrinking sphere */
  197.   glutSolidSphere (u/8.0, 5, 5);
  198.  
  199.   /* Now draw the fancier, alpha-blended gas cloud */
  200.   glEnable (GL_BLEND);
  201.   glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  202.  
  203.   /* Make a bunch of pretty sparkles */
  204.   glBegin (GL_POINTS);
  205.   for (i=0; i<20; i++)
  206.   {
  207.     Vmul (v1, boom_data[i], t);
  208.     glColor4d (boom_color[i][0], boom_color[i][1], boom_color[i][2], u);
  209.     glVertex3d (v1[0], v1[1], v1[2]);
  210.   }
  211.   glEnd();
  212.  
  213.   for (i=19; i>=0; i--)
  214.   {
  215.     /* Adjust size according to explosion age 
  216.      (should do this with glScale)*/
  217.     Vmul (v1, boom_data[icos_index[i][2]], t/2.0);
  218.     Vmul (v2, boom_data[icos_index[i][1]], t/2.0);
  219.     Vmul (v3, boom_data[icos_index[i][0]], t/2.0);
  220.  
  221.     glBegin (GL_POLYGON);
  222.  
  223.     glColor4d (boom_color[icos_index[i][2]][0],
  224.     boom_color[icos_index[i][2]][1],
  225.     boom_color[icos_index[i][2]][2],
  226.     u);
  227.     glVertex3d (v1[0], v1[1], v1[2]);
  228.  
  229.     glColor4d (boom_color[icos_index[i][1]][0],
  230.     boom_color[icos_index[i][1]][1],
  231.     boom_color[icos_index[i][1]][2],
  232.     u);
  233.     glVertex3d (v2[0], v2[1], v2[2]);
  234.  
  235.     glColor4d (boom_color[icos_index[i][0]][0],
  236.     boom_color[icos_index[i][0]][1],
  237.     boom_color[icos_index[i][0]][2],
  238.     u);
  239.     glVertex3d (v3[0], v3[1], v3[2]);
  240.  
  241.     glEnd();
  242.   }
  243.  
  244.   glDisable (GL_BLEND);
  245.   glDepthMask (GL_TRUE);
  246.   glEnable (GL_LIGHTING);
  247.   glPopMatrix();
  248. }
  249.